package cn.cdeden.knowledge.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ProjectName : ecm_svn02
 * @Package : com.cdyd.ecm.utils
 * @ClassName : CollectionUtils
 * @Author : Yu.
 * @Description : // TODO
 * @Date : 2023/11/14 9:26
 * @Version : 1.0
 */
public class CollectionUtilsImpl {

    public static String ADD_LIST = "add"; //添加
    public static String DEL_LIST = "del"; //删除
    public static String NOT_THE_SAME = "not_the_same"; //不一样的
    public static String THE_SAME = "the_same"; //一样的

//
//    public static Map<String,List> getComparison(List<String> a , List<String> b){
//        List<String> added = new ArrayList<>();
//        List<String> removed = new ArrayList<>();
//        // 查找添加的元素
//        for (String element : b) {
//            if (!Arrays.asList(a).contains(element)) {
//                added.add(element);
//            }
//        }
//        // 查找已删除的元素
//        for (String element : a) {
//            if (!Arrays.asList(b).contains(element)) {
//                removed.add(element);
//            }
//        }
//        Map<String, List> map = new HashMap<>();
//        map.put(ADD_LIST,added);
//        map.put(DEL_LIST,removed);
//        return map;
//    }
//


    /**
     * @param aList
     * @param bList
     * @return java.util.Map<java.lang.String, java.util.List < java.lang.String>>
     * @author Yu.
     * @Description // 比对数据
     * @Date 2024/5/11 9:53
     **/
    public static Map<String, List<String>> compare(List<String> aList, List<String> bList) {
        List<String> addList = new ArrayList();
        List<String> delList = new ArrayList();
        //找出需要添加的部分和共同的部分
        for (int i = 0; i < aList.size(); i++) {
            boolean isAdd = true;
            String aTemp = String.valueOf(aList.get(i));
            for (int j = 0; j < bList.size(); j++) {
                String bTemp = String.valueOf(bList.get(j));
                if (StringUtils.equals(aTemp, bTemp)) {
//                    updateList1.add(aTemp);
                    isAdd = false;
                }
            }
            if (isAdd) {
                addList.add(aTemp);
            }
        }
        //找出需要删除的部分和共同的部分
        for (int i = 0; i < bList.size(); i++) {
            boolean isDel = true;
            String bTemp = String.valueOf(bList.get(i));
            for (int j = 0; j < aList.size(); j++) {
                String aTemp = String.valueOf(aList.get(j));
                if (StringUtils.equals(aTemp, bTemp)) {
//                    updateList2.add(aTemp);
                    isDel = false;
                }
            }
            if (isDel) {
                delList.add(bTemp);
            }
        }
        Map<String, List<String>> map = new HashMap<>();
        map.put(ADD_LIST, addList);
        map.put(DEL_LIST, delList);
        map.put(NOT_THE_SAME, Stream.concat(addList.stream(), delList.stream()).collect(Collectors.toList()));
//        map.put(THE_SAME,null);
        return map;
    }


    public static void main(String[] args) {
        String[] A = {"1", "2", "3", "5"}; // array A
        String[] B = {"3", "4", "5", "6", "7"}; // array B
        List<String> added = Arrays.asList(B);
        List<String> removed = Arrays.asList(A);

        Map<String, List<String>> compare = compare(added, removed);


        System.out.println("Added elements: " + compare.get(ADD_LIST));
        System.out.println("Removed elements: " + compare.get(DEL_LIST));
        System.out.println("NOT_THE_SAME elements: " + compare.get(NOT_THE_SAME));
//        System.out.println("Removed 不相同的: " + compare.get(NOT_THE_SAME));
    }

}
